<template>
  <div class="result">
    <van-nav-bar
      :title="result+' 的搜索结果'"
      left-arrow
      fixed
      @click-left="$router.back()"
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <!-- 为什么的item.art_id不是大数，也给我报我的key不是String/number -->
      <!-- 只要循环中有一个为大数就报错 -->
      <van-cell v-for="item in resultList" :key="item.art_id.toString()" :title="item.title" @click="$router.push(`/article/${item.art_id.toString()}`)" />
    </van-list>
  </div>
</template>

<script>
import { reqGetResult } from '@/api/search'
export default {
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      resultList: [],
      page: 1
    }
  },
  computed: {
    result() {
      return this.$route.query.keyWord
    }
  },

  methods: {
    async onLoad() {
      // 异步更新数据
      const res = await reqGetResult(this.page, 15, this.result)
      //   页面一直都是15不够 显示   然后赋值又是15  死循环
      //   this.resultList = res.data.results
      this.page++
      this.resultList = [...this.resultList, ...res.data.results]
      // this.resultList.forEach(item => {
      //   console.log(item.art_id)
      //   console.log(typeof item.art_id.toString())
      // })
      this.loading = !this.loading
      if (this.resultList.length === 0 || this.resultList.length > 50) {
        this.finished = true
      }
    }
  }
}
</script>

<style scoped>
.result{
    padding-top: 46px;
}
</style>
